package com.dd.admin.controller.setting;

import cn.dev33.satoken.annotation.SaIgnore;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dd.common.convert.SysDictDataConvert;
import com.dd.common.dto.system.dict.SysDictDataDTO;
import com.dd.common.dto.system.dict.SysDictDataQueryDTO;
import com.dd.common.entity.SysDictData;
import com.dd.common.pojo.dto.PageDTO;
import com.dd.common.pojo.dto.group.CreateGroup;
import com.dd.common.pojo.dto.group.UpdateGroup;
import com.dd.common.service.SysDictDataService;
import com.dd.common.utils.AjaxResult;
import com.dd.common.utils.PageResult;
import com.dd.common.validator.annotation.IDMust;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author cheng
 */
@SaIgnore
@Tag(name = "字典数据管理")
@Validated
@RequestMapping("/setting/dict/data")
@RestController
public class SysDictDataController {
    @Resource
    private SysDictDataService sysDictDataService;
    @Resource
    private SysDictDataConvert sysDictDataConvert;

    @Operation(summary = "字典数据新增", requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(
            description = "字典数据",
            required = true,
            content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, schema = @Schema(implementation = SysDictDataDTO.class))
    ))
    @PostMapping("/add")
    public AjaxResult<Boolean> add(@Validated(CreateGroup.class) @RequestBody SysDictDataDTO dto) {
        return AjaxResult.success(sysDictDataService.save(sysDictDataConvert.dtoToEntity(dto)));
    }

    @Operation(summary = "字典数据编辑", requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(
            description = "字典数据",
            content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, schema = @Schema(implementation = SysDictDataDTO.class))
    ))
    @PatchMapping("/edit")
    public AjaxResult<Boolean> edit(@Validated(UpdateGroup.class) @RequestBody SysDictDataDTO dto) {
        return AjaxResult.success(sysDictDataService.updateById(sysDictDataConvert.dtoToEntity(dto)));
    }
    @Operation(summary = "字典数据列表")
    @GetMapping("/list")
    public AjaxResult<List<SysDictData>> list(@Validated SysDictDataQueryDTO dto) {
        LambdaQueryWrapper<SysDictData> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(dto.getDictLabel()), SysDictData::getDictLabel, dto.getDictLabel())
                .eq(dto.getEnabled() != null, SysDictData::getEnabled, dto.getEnabled())
                .eq(dto.getIsDefault() != null, SysDictData::getIsDefault, dto.getIsDefault())
                .orderByAsc(SysDictData::getId);
        return AjaxResult.success(sysDictDataService.list(queryWrapper));
    }
    @Operation(summary = "字典数据分页列表")
    @GetMapping("/page")
    public AjaxResult<PageResult<SysDictData>> pageQuery(@Validated PageDTO pageDTO, @Validated SysDictDataQueryDTO dto) {
        Page<SysDictData> page = Page.of(pageDTO.getCurrent(), pageDTO.getSize());
        LambdaQueryWrapper<SysDictData> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(dto.getDictLabel()), SysDictData::getDictLabel, dto.getDictLabel())
                .eq(dto.getEnabled() != null, SysDictData::getEnabled, dto.getEnabled())
                .eq(dto.getIsDefault() != null, SysDictData::getIsDefault, dto.getIsDefault())
                .orderByAsc(SysDictData::getId);
        IPage<SysDictData> result = sysDictDataService.page(page, queryWrapper);
        return AjaxResult.success(PageResult.iPageHandle(result.getTotal(), result.getCurrent(), result.getSize(), result.getRecords()));
    }

    @Operation(summary = "字典数据删除", parameters = @Parameter(name = "id", in = ParameterIn.PATH, description = "字典数据ID"))
    @DeleteMapping("/delete/{id}")
    public AjaxResult<Boolean> delete(@PathVariable @IDMust Long id) {
        return AjaxResult.success(sysDictDataService.removeById(id));
    }
    @Operation(summary = "字典数据详情", parameters = @Parameter(name = "id", in = ParameterIn.PATH, description = "字典数据ID"))
    @GetMapping("/detail/{id}")
    public AjaxResult<SysDictData> detail(@PathVariable @IDMust Long id) {
        return AjaxResult.success(sysDictDataService.getById(id));
    }

}
